package com.wiegand.gui.button {
	import com.wiegand.event.ClickEvent;
	import com.wiegand.gui.button.views.AbstractButtonView;
	import com.wiegand.gui.button.views.ButtonViewClasses;

	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;

	/**
	 * @author kwiegand
	 */
	 
	public class Button extends Sprite{

		protected var _text : String;
		protected var _viewName : String;
		protected var _styleName : String;
		protected var _name : String;
		protected var _view : AbstractButtonView;
		protected var _xml : XMLList;
		protected var _active : Boolean = false;
		protected var _url : String;
		protected var _event : String;
		private var _target : XMLList;		
		private var _bt : SimpleButton;

		public function Button( xml : XMLList ):void{
		
			_xml = xml;

			splitXml();
		
		}
		
		private function splitXml():void{
								
			_text = _xml;		
			_name = _xml.attribute( "name" );
			_viewName = _xml.attribute( "view" );
			_styleName = _xml.attribute( "style" );
			_url = _xml.attribute( "url" );
			_target = _xml.attribute( "target" );
			_event = _xml.attribute( "event" );
			
//			trace( "--- button --------------" );
//			trace( "_text "+_text );
//			trace( "_name "+_name );
//			trace( "_viewName "+_viewName );
//			trace( "_styleName "+_styleName );
//			trace( "_url "+_url );
//			trace( "_event "+_event );
//			trace( "--- button --------------" );

			init();
		
		}
		
		protected function init():void{
				
			_bt = new SimpleButton();
			
			_bt.addEventListener( MouseEvent.CLICK, click );
					
			_bt.name = _name;			
						
			var viewClass : Class = ButtonViewClasses.getClass( _viewName );
			
			_view = new viewClass();
			
			setStates();
			
			addChild( _bt );	
				
		}
		
		protected function click( e : MouseEvent = null ) : void {
			
			if( _url != "" && _url != null && _url.length != 0 ) goToUrl();
			else dispatch();
			
		}

		private function setStates( e : MouseEvent = null ) : void {
			
			_bt.upState = _view.getUpState( _xml );			
			_bt.overState = _view.getOverState( _xml ); 
			_bt.downState = _view.getDownState( _xml ); 					
			_bt.hitTestState = _bt.upState;		
			
		}
		
		private function setDownState( e : MouseEvent = null ):void{	
				
			_bt.upState = _view.getDownState( _xml  );
			_bt.overState = _view.getDownState( _xml  );
			
		}
				
		public function setActive( b : Boolean ):void{
						
			_active = b;
		
			if( b ){
			
				setDownState();
				_bt.addEventListener( MouseEvent.MOUSE_OVER, setDownState );
				_bt.addEventListener( MouseEvent.MOUSE_OUT, setDownState );
			
			}
			else{
				
				setStates();
				_bt.removeEventListener( MouseEvent.MOUSE_OVER, setDownState );
				_bt.removeEventListener( MouseEvent.MOUSE_OUT, setDownState );
			
			}
		
		}
		
		public function getActive() : Boolean{
		
			return _active;
		
		}
		
		public function getUrl() : String{
		
			return _url;
		
		}
		
		public function getEvent() : String{
		
			return _event;
		
		}
		
		public function getName() : String{
		
			return _name;
		
		}
		
		public function getText(): String{
			
			return _text;
			
		}
		
		public function getStyle():String{
			
			return _styleName;
			
		}
		
		private function goToUrl() : void{
			
			var target : String;
			
			switch( _target ){
				
				case "self"	:		target = "_self";
									break; 
				case "_self"	:	target = "_self";
									break; 
				case "parent"	:	target = "_parent";
									break; 
				case "_parent"	:	target = "_parent";
									break; 
				case "top"		:	target = "_top";
									break; 
				case "_top"		:	target = "_top";
									break; 
				case "blank"	:	target = "_blank";
									break; 
				case "_blank"	:	target = "_blank";
									break; 
				default			:	target = "_self";
									break; 
			
			}
			
//			_blank, um den Verweis in einem neuen Fenster zu öffnen,
//			_self, um den Verweis im aktuellen Fenster zu öffnen,
//			_parent, um bei verschachtelten Framesets das aktuelle Frameset zu sprengen,
//			_top, um bei verschachtelten Framesets alle Framesets zu sprengen.
			
			var request : URLRequest = new URLRequest( _url );
   			navigateToURL( request, target );
   			
//   			trace( "Button goToUrl: " + _url+", "+target );
		
		}
		
		private function dispatch() : void{
		
			dispatchEvent( new ClickEvent( _event, _name ));
		
		}
		
		public function destroy():void{
					
//			trace( "button ---> : " + _bt );
			
//			_bt.removeEventListener( MouseEvent.CLICK, click );
//			_bt.removeEventListener( MouseEvent.MOUSE_OVER, setDownState );
//			_bt.removeEventListener( MouseEvent.MOUSE_OUT, setDownState );
//			
//			removeChild( _bt );
			
			_bt = null;
			_text = null;
			_name = null;
			_viewName = null;
			_styleName = null;
			_url = null;
			_event = null;
		
		}
		
	}
	
}
